# -*- coding: UTF-8 -*-
import cookielib
import re
import urllib
import urllib2


# module invoke,different import mode
def import_module():
    from t.module import function_name
    function_name();
    
    import t.module  as mr;
    mr.function_name();
    
    import t.module;
    t.module.function_name();
    
    # define and invoke function (with default argument)
    def func(v1, v2=0):
        return v1 + v2;
    
    print func(5, 6);
    print func(5);
    
    
def pyweb_login():
    values = {u'username':'admin', u'password':'root', u'csrfmiddlewaretoken':'6mQsH2svplV69gRAdBEb3aDEeqaUyERY', u'next':'/admin/'}
    values = urllib.urlencode(values)
    header = {
              'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.116 Safari/537.36',
              'Referer':'http://localhost:8000/admin/login/?next=/admin/',
              'Cookie':'JSESSIONID=D7F205E6151F316CA285B1E4168C9FED; sessionid=d63ox6xm8vi5aa9k5xchi6vdoyk5keba; csrftoken=6mQsH2svplV69gRAdBEb3aDEeqaUyERY'
              }
    login_url = "http://localhost:8000/admin/login/?next=/admin/"
    req = urllib2.Request(url=login_url, data=values, headers=header)
      
    rsp = urllib2.urlopen(req)
    html = rsp.read()
    print html  
    rsp = urllib2.urlopen('http://localhost:8000/admin/')
    html = rsp.read()
    print html;
    

def baidu_login():
    url_index = u'http://www.baidu.com'
    url_token = u'https://passport.baidu.com/v2/api/?getapi&tpl=pp&apiver=v3&class=login'
    url_login = u'https://passport.baidu.com/v2/api/?login'
    username = u'1509788891@qq.com'
    password = u'12345671'
    
    cj = cookielib.LWPCookieJar()
    cookies = urllib2.HTTPCookieProcessor(cj)
    opener = urllib2.build_opener(cookies, urllib2.HTTPHandler)
    urllib2.install_opener(opener)
    
    # 获取百度默认的cookie
    rsp_index = urllib2.urlopen(url_index)
    print rsp_index.info()
    
    rsp_token = urllib2.urlopen(url_token)
    
    match_val = re.search(u'"token" : "(?P<tokenVal>.*?)"', rsp_token.read())
    token_val = match_val.group('tokenVal')
    print token_val
    
    param = {
             u'username':username,
             u'password':password,
             u'u':'https://passport.baidu.com',
             u'tpl':'pp',
             u'token':token_val,
             u'staticpage':'https://passport.baidu.com/static/passpc-account/html/v3Jump.html',
             u'isPhone':'false',
             u'charset':'UTF-8',
             u'callback':'parent.bd_pcbs-ra48vi'
    }
    param = urllib.urlencode(param)
    
    #
    req_login = urllib2.Request(url_login, param);
    req_login.add_header('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8');
    req_login.add_header('Accept-Encoding', 'gzip,deflate,sdch');
    req_login.add_header('Accept-Language', 'zh-CN,zh;q=0.8');
    req_login.add_header('User-Agent', 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.72 Safari/537.36');
    req_login.add_header('Content-Type', 'application/x-www-form-urlencoded');
    
    # 获取百度用户登录cookie
    rsp_login = urllib2.urlopen(req_login);
    print rsp_login.info()


